$(function () {
    function baiduMapFun() {
        // 百度地图API功能
        var map = new BMap.Map("findCar", {mapType: BMAP_HYBRID_MAP}); // 创建Map实例
        var centerPoint = new BMap.Point(108.946306, 34.347436);
        var local = new BMap.LocalSearch(map, {
            renderOptions: {map: map, panel: "r-result"}
        });
        var navigationControl = new BMap.NavigationControl({
            anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
            type: BMAP_NAVIGATION_CONTROL_LARGE,
            enableGeolocation: true
        });
        function baiduMapSet() {
            map.centerAndZoom(centerPoint, 12);// 设置地图的中心和缩放的等级
            map.addControl(navigationControl);//添加地图缩放和方向控件
            map.addControl(new BMap.MapTypeControl());//添加地图浏览模式
            map.enableScrollWheelZoom(true);//滚轮放大缩小
            map.disableDragging();//禁止拖拽
            setTimeout(function () {
                map.enableDragging();   // 开启拖拽
            }, 300);
            map.setViewport(pointArray);//让所有点在视野范围内
        }


        var geoc = new BMap.Geocoder();//地理代码
        var json_data = [[116.404, 39.915], [116.383752, 39.91334], [116.384502, 39.932241]];
        var pointArray = new Array();
        angular.element('.position_info_box').hide();



        function addMarker() {
            for (var i = 0; i < json_data.length; i++) {
                var marker = new BMap.Marker(new BMap.Point(json_data[i][0], json_data[i][1])); // 创建点
                map.addOverlay(marker);    //增加点
                pointArray[i] = new BMap.Point(json_data[i][0], json_data[i][1]);
                marker.addEventListener("click", attribute);
            }
        }


        /*获取覆盖物位置*/
        function attribute(e) {
            var p = e.target;
            // console.log("marker的位置是" + p.getPosition().lng + "," + p.getPosition().lat);
            $('.position_info_box').show();
            var jing_du_value = e.point.lng;
            var wei_du_value = e.point.lat;
            // var pt = new BMap.Point(jing_du_value, wei_du_value);
            // $("#spn1").text("经度:" + jing_du_value);
            // $("#spn2").text("纬度:" + wei_du_value);
            console.log(e);
            var carPoint = e.point;
            geoc.getLocation(carPoint, function (rs) {
                //addressComponents对象可以获取到详细的地址信息
                var addComp = rs.addressComponents;
                var carSite;
                if (addComp.province === "") {
                    carSite = "地址:" + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                } else if (addComp.city === "") {
                    carSite = "地址:" + addComp.province + addComp.district + addComp.street + addComp.streetNumber;
                } else if (addComp.district === "") {
                    carSite = "地址:" + addComp.province + addComp.city + addComp.street + addComp.streetNumber;
                } else if (addComp.street === "") {
                    carSite = "地址:" + addComp.province + addComp.city + addComp.district + addComp.streetNumber;
                } else if (addComp.streetNumber === "") {
                    carSite = "地址:" + addComp.province + addComp.city + addComp.district + addComp.street;
                } else {
                    carSite = "地址:" + addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                }

                //将对应的HTML元素设置值
                console.log(carSite);
                console.log(carPoint);
                $(".position_info_box .car_addr").text(carSite);
            });
        }

        baiduMapSet();
        addMarker();


         var btn = document.getElementById('btn');
         var text = document.getElementById('search_ipt');
         btn.onclick = function () {
             var txt = text.value;
             local.search(txt);
         }

    }
});